
package com.redside.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.redside.Constants;
import com.redside.dao.ComplaintDao;
import com.redside.dao.InitialComplaintDao;
import com.redside.model.Complaint;
import com.redside.model.Correspondence;
import com.redside.model.InitialComplaint;
import com.redside.model.list.OptionsBean;
import com.redside.service.ComplaintManager;
import com.redside.service.OptionsBeanLookupManager;
import com.redside.view.AssociatedComplaint;

/**
 * Implementation of InitialComplaintManager interface.
 * 
 * @author <a href="mailto:pchapman@easystreet.com">Peter Chapman</a>
 */
public class ComplaintManagerImpl extends GenericManagerImpl<Complaint,Long> implements ComplaintManager {

    public ComplaintManagerImpl(ComplaintDao dao, InitialComplaintDao iDao) {
		super(dao);
		this.dao=dao;
		this.iDao=iDao;
	}

	private ComplaintDao dao;
	private InitialComplaintDao iDao;
    private OptionsBeanLookupManager optionsBeanManager;

    public void setOptionsBeanManager(OptionsBeanLookupManager optionsBeanManager) {
        this.optionsBeanManager = optionsBeanManager;
    }

    public List<Complaint> findAllUnverified() {
        return dao.findAllUnverified();
    }

    public Complaint save(Complaint complaint){
    	logger.debug("SAVING COMPLAINT IN MANAGER............................");
    	logger.debug("InitialComplaint id: "+complaint.getInitialComplaint().getId());
    	InitialComplaint ic = iDao.save(complaint.getInitialComplaint());
    	logger.debug("InitialComplaint id: "+ic.getId());
    	complaint.setInitialComplaint(ic);
    	Complaint c = dao.save(complaint);
    	return c;
    }
    
/*    public List<CitationListView> findCitationComplaintsByCountyId(String id) {
        List<Complaint> citations = dao.findCitationComplaintsByCountyId(id);

        List<CitationListView> citationViews = new ArrayList<CitationListView>();
        
        
        
        for (Complaint complaint : citations) {

            Date siteVisitDate = null;
            Date reqImplementationDate = null;
            Date followUpDate = null;
            
            if(complaint.getSiteVisits() != null){
                if(complaint.getSiteVisits().getVisitDate() != null){
                    siteVisitDate=complaint.getSiteVisits().getVisitDate();
                    logger.debug("here is a getVisitDate: " + siteVisitDate);
                }
            }
            if(complaint.getRemediations() != null){
                if(complaint.getRemediations().getReqImplementationDate() != null){
                    reqImplementationDate=complaint.getRemediations().getReqImplementationDate();
                    logger.debug("here is a getReqImplementationDate: "+ reqImplementationDate);
                }
                if(complaint.getRemediations().getFollowUpDate() != null){
                    followUpDate=complaint.getRemediations().getFollowUpDate();
                    logger.debug("here is a getFollowUpDate: "+ followUpDate);
                }
            }
            
            citationViews.add(new CitationListView(complaint.getComplaintId(), 
                    complaint.getComplaintDate(), 
                    complaint.getBusiness().getBusinessName(), 
                    complaint.getBusiness().getBusinessAddress1(), 
                    complaint.getBusiness().getBusinessCity(), 
                    complaint.getBusiness().getBusinessZip(),
                    new ComplaintStatusLkp(optionsBeanManager.findById(
                            Constants.COMPLAINT_STATUS, complaint.getComplaintStatusId())),
                    siteVisitDate, 
                    reqImplementationDate, 
                    followUpDate));
        }
        return citationViews;
    }*/
    
    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findAllForBusinessView(gov.oregon.wems.model.business.Business)
     * 
     */
    /*public List<BusinessViewComplaint> findAllForBusinessView(Long businessId) {
        List<Complaint> complaints = dao.findAllForBusinessView(businessId);
        List<BusinessViewComplaint> businessViewComplaints = new ArrayList<BusinessViewComplaint>();

        for (Complaint complaint : complaints) {
            Date irlSentDate = null;
            Date siteVisitDate = null;
            Date remediationVisitDate = null;
            Date remediationRequiredDate = null;
            Date citationIssuedDate = null;
            Date fineReceivedDate = null;
            OptionsBean nature = new OptionsBean("", "");
            OptionsBean nonActionableReason = new OptionsBean("","");
            logger.debug("===============> " + complaint.getComplaintId()
                    + " <===============");
            OptionsBean status = optionsBeanManager.findById(Constants.COMPLAINT_STATUS,
                    complaint.getComplaintStatusId());
            if (complaint.getNatureOfComplaintId() != null) {
                nature = optionsBeanManager.findById(Constants.COMPLAINT_NATURE_BEAN,
                        complaint.getNatureOfComplaintId());
                logger.debug("here is the nature: " + nature.getValue());
            }
            if(complaint.getNonActionableReasonId() != null 
                    && complaint.getNonActionableReasonId() > 0){
                nonActionableReason=optionsBeanManager.findById(Constants.NON_ACTIONABLE_BEAN,
                        complaint.getNonActionableReasonId());
            }

            logger.debug("here is the status: " + status.getValue());
            if (complaint.getSiteVisits() != null) {
                siteVisitDate = complaint.getSiteVisits().getVisitDate();
            }
            if (complaint.getRemediations() != null) {
                logger.debug("\n\n Getting remediationVisitDate: "+ complaint.getRemediations().getFollowUpDate());
                remediationVisitDate = complaint.getRemediations().getFollowUpDate();

                logger.debug("\n\n Getting remediationRequiredDate: "+ complaint.getRemediations().getReqImplementationDate());
                remediationRequiredDate = complaint.getRemediations().getReqImplementationDate();
            }
            if (complaint.getCitations() != null) {
                citationIssuedDate = complaint.getCitations().getCitationIssuedDate();
                fineReceivedDate = complaint.getCitations().getFineReceivedDate();
            }
            logger.debug("corr size: " + complaint.getCorrespondences().size());
            for (Correspondence corr : complaint.getCorrespondences()) {
                logger.debug("corr getCorrTypeLkpId: "
                        + corr.getCorrTypeLkp().getCorrTypeId());
                if (corr.getCorrTypeLkp().getCorrTypeId() != null
                        && corr.getCorrTypeLkp().getCorrTypeId() == 2) {
                    irlSentDate = corr.getSentDate();
                }
            }
            businessViewComplaints.add(new BusinessViewComplaint(complaint
                    .getComplaintId(), complaint.getComplaintDate(), 
                    complaint.getComplaintStatusId(), status.getValue(),
                    nature.getValue(), irlSentDate, siteVisitDate, remediationRequiredDate,
                    remediationVisitDate, citationIssuedDate, fineReceivedDate,
                    complaint.isActionable(), nonActionableReason.getValue(), complaint.isFirstValidComplaint()));
        }
        return businessViewComplaints;
    }*/

    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findByCountyId()
     */
    public List<Complaint> findByCountyId(String id) {
        return dao.findByCountyId(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findUnverifiedByCountyId(java.lang.Long)
     */
    public List<Complaint> findUnverifiedByCountyId(String id) {
        return dao.findUnverifiedByCountyId(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findUnsentIRL()
     */
/*    public List<UnsentIRLView> findUnsentIRL(String countyId, Long corresponceTypeId,
            String sql1) {
        return daoSpring.findUnsentIRL(countyId, corresponceTypeId, sql1);
    }
*/
    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findUnsentIRL()
     */
/*    public List<UnsentIRLView> findUnsentIRL(Long corresponceTypeId, String sql1) {
        return daoSpring.findUnsentIRL(corresponceTypeId, sql1);
    }
*/
    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findComplaintsForVerifyProcess(java.lang.Long)
     */
    public List<AssociatedComplaint> findComplaintsForVerifyProcess(Long id) {
        List<Complaint> complaints = dao.findComplaintsForVerifyProcess(id);
        List<AssociatedComplaint> associatedList = new ArrayList<AssociatedComplaint>();
        Date irlSentDate = null;
        for (Complaint complaint : complaints) {
            for (Correspondence corr : complaint.getCorrespondences()) {
                if (corr.getCorrTypeLkpId() != null
                        && corr.getCorrTypeLkpId() == 2) {
                    irlSentDate = corr.getSentDate();
                }
            }
            OptionsBean status = optionsBeanManager.findById(Constants.COMPLAINT_STATUS,
                    complaint.getComplaintStatusId());
            associatedList.add(new AssociatedComplaint(complaint.getId(),
                    complaint.getComplaintDate(), status.getValue(), irlSentDate));
        }
        return associatedList;
    }

    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#siteVisitByCountyId(java.lang.String)
     */
/*    public List<SiteVisitView> siteVisitByCountyId(String countyId) {
        return daoSpring.siteVisitByCountyId(countyId);
    }
*/
    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findAllRemediations()
     */
/*    public List<Complaint> findAllRemediations() {
        return dao.findAllRemediations();
    }
*/
    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findRemediationByCountyId(java.lang.String)
     */
    public List<Complaint> findRemediationByCountyId(String countyId) {
        return dao.findRemediationByCountyId(countyId);
    }


    /*
     * (non-Javadoc)
     * 
     * @see gov.oregon.wems.manager.complaint.ComplaintManager#findByBusinessId(java.lang.String)
     */    
    public List<Complaint> findByBusinessId(Long businessId) {
        return dao.findByBusinessId(businessId);
    }    

}